<?php

/**
 *
 * 缓存api
 * @author jeff
 *
 */
class CacheApp extends ApiApp
{
    public $_cache_server;

    function __construct()
    {
        $this->CacheApp();
    }

    function CacheApp()
    {
        parent::__construct();
        $this->_cache_server =& cache_server();
    }

    /**
     * 清除商品缓存
     * @TODO 暂时没有添加权限控制     。。。。。。。
     * powered by jeff 2011-5-31
     */
    function clear_goods_cache()
    {
        //缓存 key 前缀
        //缓存key的构造方法为 “前缀_$goods_id”
        $all_prefixes = array(
            'page_of_goods', //清除商品公共信息缓存
            'page_of_goods_sales_log', //清除商品销售记录缓存
            'page_of_goods_qa', //清除商品咨询记录缓存
            'page_goods_category', //清除商品分类缓存
        );

        //获得goods_id  多个goods_id使用","分割
        $goods_ids = $_REQUEST['goods_ids'];

        if (empty($goods_ids)) {
            ApiError::showError('goods_id_required', 1);
            return;
        } else {
            $goods_ids = explode(',', $goods_ids);
        }

        //获得缓存前缀  多个前缀使用","分割
        $prefixes = $_REQUEST['prefix'];

        if (empty($prefixes)) {
            $prefixes = $all_prefixes;
        } else {
            if (is_string($prefixes)) {
                $prefixes = explode(',', $prefixes);
            }
        }

        foreach ($goods_ids as $goods_id) {
            foreach ($prefixes as $prefix) {
                if (!empty($goods_id) && !empty($prefix) && in_array($prefix, $all_prefixes)) {
                    $cache_key = $prefix . '_' . $goods_id;
                    $this->_cache_server->delete($cache_key);
                }
            }
        }

        return json_encode(true);
    }

}